<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">


      <el-form-item label="检索信息" prop="search">
        <el-input v-model="queryParams.search" placeholder="请输入检索信息" clearable
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="标签" prop="label">
        <el-select v-model="queryParams.label" placeholder="请选择">
          <el-option label="学校" value="0"></el-option>
          <el-option label="家" value="1"></el-option>
          <el-option label="公司" value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="地址" prop="code">
        <el-cascader @change="handleChange" v-model="queryParams.code" :options="data" :props="props" collapse-tags
                     clearable>
        </el-cascader>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['store:address:add']">新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
                   v-hasPermi="['store:address:edit']">修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
                   v-hasPermi="['store:address:remove']">删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="addressList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column type="index" width="50" label="编号"/>
      <el-table-column label="用户" align="center" prop="memberName"/>
      <el-table-column label="收货人" align="center" prop="consignee"/>
      <el-table-column label="省级" align="center" prop="provinceName">
        <template slot-scope="scope">
          <el-tag effect="dark" type="danger" v-if="scope.row.provinceName != null">
            {{ scope.row.provinceName }}
          </el-tag>
          <el-tag effect="dark" type="info" v-else>暂无省份</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="市级" align="center" prop="cityName">
        <template slot-scope="scope">
          <el-tag effect="dark" type="danger" v-if="scope.row.cityName != null">
            {{ scope.row.cityName }}
          </el-tag>
          <el-tag effect="dark" type="info" v-else>暂无市级</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="区级" align="center" prop="districtName">
        <template slot-scope="scope">
          <el-tag effect="dark" type="danger" v-if="scope.row.districtName != null">
            {{ scope.row.districtName }}
          </el-tag>
          <el-tag effect="dark" type="info" v-else>暂无区级</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="详细地址" align="center" prop="detail">
        <template slot-scope="scope">
          <el-tag effect="dark" type="danger" v-if="scope.row.detail != null">
            {{ scope.row.detail }}
          </el-tag>
          <el-tag effect="dark" type="info" v-else>暂无区域</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="区域编码" align="center" prop="areaCode">
        <template slot-scope="scope">
          <el-tag effect="dark" type="danger" v-if="scope.row.areaCode != null">
            {{ scope.row.areaCode }}
          </el-tag>
          <el-tag effect="dark" type="info" v-else>暂无编码</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="标签" align="center" prop="label">
        <template slot-scope="scope">
          <el-tag effect="dark" :type="scope.row.label == '1' ? 'danger' : (scope.row.label == '2' ? 'info' : '')">
            {{ scope.row.label == '1' ? '家' : (scope.row.label == '2' ? '公司' : '学校') }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="收货手机号" align="center" prop="phone"/>
      <el-table-column label="默认地址" align="center" prop="flag">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.flag" active-value="1" inactive-value="0" @change="handleFlagChange(scope.row)">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['store:address:edit']">修改
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['store:address:remove']">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>

    <!-- 添加或修改收货地址对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户" prop="memberId">
          <el-select v-model="form.memberId" placeholder="请选择">
            <el-option v-for="item in options" :key="item.id" :label="item.nickName" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="收货人" prop="consignee">
          <el-input v-model="form.consignee" placeholder="请输入收货人"/>
        </el-form-item>
        <el-form-item label="地址" prop="code">
          <el-cascader ref="cascader" @change="handleChange1" v-model="form.areaCode" :options="data" :props="props"
                       collapse-tags clearable>
          </el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="detail">
          <el-input v-model="form.detail" placeholder="请输入详细地址"/>
        </el-form-item>
        <el-form-item label="标签" prop="label">
          <el-radio-group v-model="form.label">
            <el-radio :label="'0'">学校</el-radio>
            <el-radio :label="'1'" checked>家</el-radio>
            <el-radio :label="'2'" checked>公司</el-radio>
          </el-radio-group>

        </el-form-item>
        <el-form-item label="联系方式" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入收获手机号"/>
        </el-form-item>
        <el-form-item label="是否默认" prop="flag">
          <el-radio-group v-model="form.flag">
            <el-radio :label="'0'">不默认</el-radio>
            <el-radio :label="'1'" checked>默认</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input type="textarea" v-model="form.remark" placeholder="请输入备注"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  changeAddressFlag,
  getAddressAndCode,
  listAddress,
  getAddress,
  delAddress,
  addAddress,
  updateAddress
} from "@/api/store/address";
import {getMemberList} from "@/api/store/member";

export default {
  name: "Address",
  data() {
    return {
      // 会员信息
      options: [],
      // 分类 级联数据
      props: {
        children: 'children',
        label: 'name',
        value: "code",
      },
      data: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 收货地址表格数据
      addressList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        memberId: [
          {required: true, message: "用户id不能为空", trigger: "blur"}
        ],
        phone: [
          {required: true, message: "收获手机号不能为空", trigger: "blur"}
        ],
        createTime: [
          {required: true, message: "创建时间不能为空", trigger: "blur"}
        ],
        updateTime: [
          {required: true, message: "修改时间不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {

    this.getList();
    getAddressAndCode().then(res => {
      this.data = res
    })
  },
  methods: {
    handleChange1(val) {
      this.form.areaCode = val[2]
      let nodesInfo = this.$refs['cascader'].getCheckedNodes()
      console.log(nodesInfo);
      this.form.districtName = nodesInfo[0].label
      this.form.cityName = nodesInfo[0].parent.label
      this.form.provinceName = nodesInfo[0].parent.parent.label
    },
    // 修改地址默认
    handleFlagChange(row) {
      let text = row.flag === "1" ? "默认" : "停用";
      this.$modal.confirm('确认要设置"' + text + row.memberName + '"吗？').then(function () {
        return changeAddressFlag(row.id, row.flag);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
        this.getList()
      }).catch(function () {
        row.flag = row.flag === "0" ? "1" : "0";
      });
    },
    handleChange(val) {
      this.queryParams.code = val[2]
    },
    /** 查询收货地址列表 */
    getList() {
      this.loading = true;
      listAddress(this.queryParams).then(response => {
        this.addressList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        label: '1',
        flag: '1'
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {

      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        name: null,
        code: ""
      }
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    // 获取会员信息
    MemberList() {
      getMemberList().then(res => {
        this.options = res.data
      })
    },
    /** 新增按钮操作 */
    handleAdd() {

      this.reset();
      this.MemberList();
      this.open = true;
      this.title = "添加收货地址";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAddress(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改收货地址";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          addAddress(this.form).then(response => {
            this.$modal.msgSuccess("新增成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除收货地址编号为"' + ids + '"的数据项？').then(function () {
        return delAddress(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },

  }
};
</script>
